home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / syntax.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-26  |  42.8 KB  |  1,555 lines

  1. /* XEmacs routines to deal with syntax tables; also word and list parsing.
  2.    Copyright (C) 1985-1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of XEmacs.
  5.  
  6. XEmacs is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14. for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with XEmacs; see the file COPYING.  If not, write to the Free
  18. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Synched up with: FSF 19.28. */
  21.  
  22. #include <config.h>
  23. #include "lisp.h"
  24.  
  25. #include "buffer.h"
  26. #include "commands.h"
  27. #include "insdel.h"
  28. #include "syntax.h"
  29.  
  30. Lisp_Object Qsyntax_table_p;
  31.  
  32. int words_include_escapes;
  33.  
  34. int parse_sexp_ignore_comments;
  35.  
  36. int no_quit_in_re_search;
  37.   
  38. Lisp_Object Vstandard_syntax_table;
  39.  
  40. /* !!#### A vector is not sufficient for Mule characters.
  41.    What we need is a data structure that allows efficient
  42.    definition/lookup of ranges of integers.  This could
  43.    also be used for display tables.
  44.  
  45.    A hashtable is not sufficient; that doesn't allow for
  46.    ranges.
  47.  
  48.    A reasonable structure is a sorted gap array where each element
  49.    is a range.  Need to define a type like this.
  50.  */
  51.  
  52. /* This is the internal form of the parse state used in parse-partial-sexp.  */
  53.  
  54. struct lisp_parse_state
  55.   {
  56.     int depth;        /* Depth at end of parsing */
  57.     int instring;    /* -1 if not within string, else desired terminator. */
  58.     int incomment;    /* Nonzero if within a comment at end of parsing */
  59.     int comstyle;    /* comment style a=0, or b=1 */
  60.     int quoted;        /* Nonzero if just after an escape char at end of parsing */
  61.     Bufpos thislevelstart;/* Char number of most recent start-of-expression at current level */
  62.     Bufpos prevlevelstart;/* Char number of start of containing expression */
  63.     Bufpos location;    /* Char number at which parsing stopped. */
  64.     int mindepth;    /* Minimum depth seen while scanning.  */
  65.     Bufpos comstart;    /* Position just after last comment starter.  */
  66.   };
  67.  
  68. /* These variables are a cache for finding the start of a defun.
  69.    find_start_pos is the place for which the defun start was found.
  70.    find_start_value is the defun start position found for it.
  71.    find_start_buffer is the buffer it was found in.
  72.    find_start_begv is the BEGV value when it was found.
  73.    find_start_modiff is the value of MODIFF when it was found.  */
  74.  
  75. static Bufpos find_start_pos;
  76. static Bufpos find_start_value;
  77. static struct buffer *find_start_buffer;
  78. static Bufpos find_start_begv;
  79. static int find_start_modiff;
  80.  
  81. /* Find a defun-start that is the last one before POS (or nearly the last).
  82.    We record what we find, so that another call in the same area
  83.    can return the same value right away.  */
  84.  
  85. static Bufpos
  86. find_defun_start (struct buffer *buf, Bufpos pos)
  87. {
  88.   int tem;
  89.   Lisp_Object table = buf->syntax_table;
  90.  
  91.   /* Use previous finding, if it's valid and applies to this inquiry.  */
  92.   if (buf == find_start_buffer
  93.       /* Reuse the defun-start even if POS is a little farther on.
  94.      POS might be in the next defun, but that's ok.
  95.      Our value may not be the best possible, but will still be usable.  */
  96.       && pos <= find_start_pos + 1000
  97.       && pos >= find_start_value
  98.       && BUF_BEGV (buf) == find_start_begv
  99.       && BUF_MODIFF (buf) == find_start_modiff)
  100.     return find_start_value;
  101.  
  102.   /* Back up to start of line.  */
  103.   tem = find_next_newline (buf, pos, -1);
  104.  
  105.   while (tem > BUF_BEGV (buf))
  106.     {
  107.       /* Open-paren at start of line means we found our defun-start.  */
  108.       if (SYNTAX (table, BUF_FETCH_CHAR (buf, tem)) == Sopen)
  109.     break;
  110.       /* Move to beg of previous line.  */
  111.       tem = find_next_newline (buf, tem, -2);
  112.     }
  113.  
  114.   /* Record what we found, for the next try.  */
  115.   find_start_value = tem;
  116.   find_start_buffer = buf;
  117.   find_start_modiff = BUF_MODIFF (buf);
  118.   find_start_begv = BUF_BEGV (buf);
  119.   find_start_pos = pos;
  120.  
  121.   return find_start_value;
  122. }
  123.  
  124. DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
  125.   "Return t if ARG is a syntax table.\n\
  126. Any vector of 256 elements will do.")
  127.   (obj)
  128.      Lisp_Object obj;
  129. {
  130.   if (VECTORP (obj) && vector_length (XVECTOR (obj)) == 0400)
  131.     return Qt;
  132.   return Qnil;
  133. }
  134.  
  135. static Lisp_Object
  136. check_syntax_table (Lisp_Object obj, Lisp_Object def)
  137. {
  138.   if (NILP (obj))
  139.     obj = def;
  140.   while (NILP (Fsyntax_table_p (obj)))
  141.     obj = wrong_type_argument (Qsyntax_table_p, obj);
  142.   return (obj);
  143. }
  144.  
  145. DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 1, 0,
  146.   "Return the current syntax table.\n\
  147. This is the one specified by the current buffer, or by BUFFER if it\n\
  148. is non-nil.")
  149.   (buffer)
  150.   Lisp_Object buffer;
  151. {
  152.   return decode_buffer (buffer, 0)->syntax_table;
  153. }
  154.  
  155. DEFUN ("standard-syntax-table", Fstandard_syntax_table,
  156.    Sstandard_syntax_table, 0, 0, 0,
  157.   "Return the standard syntax table.\n\
  158. This is the one used for new buffers.")
  159.   ()
  160. {
  161.   return Vstandard_syntax_table;
  162. }
  163.  
  164. DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
  165.   "Construct a new syntax table and return it.\n\
  166. It is a copy of the TABLE, which defaults to the standard syntax table.")
  167.   (table)
  168.      Lisp_Object table;
  169. {
  170.   if (NILP (Vstandard_syntax_table))
  171.     /* Can only be null during initialization */
  172.     return make_vector (0400, Qzero);
  173.  
  174.   table = check_syntax_table (table, Vstandard_syntax_table);
  175.   return Fcopy_sequence (table);
  176. }
  177.  
  178. DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 2, 0,
  179.   "Select a new syntax table for BUFFER.\n\
  180. One argument, a syntax table.\n\
  181. BUFFER defaults to the current buffer if omitted.")
  182.   (table, buffer)
  183.      Lisp_Object table, buffer;
  184. {
  185.   struct buffer *buf = decode_buffer (buffer, 0);
  186.   table = check_syntax_table (table, Qnil);
  187.   buf->syntax_table = table;
  188.   /* Indicate that this buffer now has a specified syntax table.  */
  189.   buf->local_var_flags |= XINT (buffer_local_flags.syntax_table);
  190.   return table;
  191. }
  192.  
  193. /* Convert a letter which signifies a syntax code
  194.  into the code it signifies.
  195.  This is used by modify-syntax-entry, and other things. */
  196.  
  197. CONST unsigned char syntax_spec_code[0400] =
  198.   { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
  199.     0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
  200.     0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
  201.     0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
  202.     (char) Swhitespace, 0377, (char) Sstring, 0377,
  203.         (char) Smath, 0377, 0377, (char) Squote,
  204.     (char) Sopen, (char) Sclose, 0377, 0377,
  205.     0377, (char) Swhitespace, (char) Spunct, (char) Scharquote,
  206.     0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
  207.     0377, 0377, 0377, 0377,
  208.     (char) Scomment, 0377, (char) Sendcomment, 0377,
  209.     (char) Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377,   /* @, A ... */
  210.     0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
  211.     0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
  212.     0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol,
  213. #ifndef MULE
  214.     0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,   /* `, a, ... */
  215. #else
  216.     0377, 0377, 0377, 0377, 0377, (char) Sextword,
  217.       0377, 0377, /* `, a, ... */
  218. #endif /* MULE */
  219.     0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
  220.     0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
  221.     0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377
  222.   };
  223.  
  224. /* Indexed by syntax code, give the letter that describes it. */
  225.  
  226.  
  227. #ifdef MULE
  228. CONST unsigned char syntax_code_spec[15] =
  229.   {
  230.     ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@', 'e'
  231.   };
  232. #else
  233. CONST unsigned char syntax_code_spec[14] =
  234.   {
  235.     ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@'
  236.   };
  237. #endif /* MULE */
  238.  
  239. DEFUN ("syntax-designator-chars", Fsyntax_designator_chars,
  240.        Ssyntax_designator_chars, 0, 0, 0,
  241.   "Return a string of the recognized syntax designator chars.\n\
  242. The chars are ordered by their internal syntax codes, which are\n\
  243. numbered starting at 0.")
  244.   ()
  245. {
  246.   return make_string (syntax_code_spec, 1 + Smax);
  247. }
  248.  
  249. DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 2, 0,
  250.   "Return the syntax code of CHAR, described by a character.\n\
  251. For example, if CHAR is a word constituent, the character `?w' is returned.\n\
  252. The characters that correspond to various syntax codes\n\
  253. are listed in the documentation of `modify-syntax-entry'.\n\
  254. Optional second argument TABLE defaults to the current buffer's\n\
  255. syntax table.")
  256.   (ch, table)
  257.      Lisp_Object ch, table;
  258. {
  259.   CHECK_COERCE_CHAR (ch, 0);
  260.   table = check_syntax_table (table, current_buffer->syntax_table);
  261.  
  262.   return make_number (syntax_code_spec[(int) SYNTAX (table, XINT (ch))]);
  263. }
  264.  
  265. DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 2, 0,
  266.   "Return the matching parenthesis of CHAR, or nil if none.\n\
  267. Optional second argument TABLE defaults to the current buffer's\n\
  268. syntax table.")
  269.   (ch, table)
  270.      Lisp_Object ch, table;
  271. {
  272.   int code;
  273.   CHECK_COERCE_CHAR (ch, 0);
  274.   table = check_syntax_table (table, current_buffer->syntax_table);
  275.   code = SYNTAX (table, XINT (ch));
  276.   if (code == Sopen || code == Sclose)
  277.     return make_number (SYNTAX_MATCH (table, XINT (ch)));
  278.   return Qnil;
  279. }
  280.  
  281.  
  282. /* Return the position across COUNT words from FROM.
  283.    If that many words cannot be found before the end of the buffer, return 0.
  284.    COUNT negative means scan backward and stop at word beginning.  */
  285.  
  286. int
  287. scan_words (struct buffer *buf, int from, int count)
  288. {
  289.   int beg = BUF_BEGV (buf);
  290.   int end = BUF_ZV (buf);
  291.   enum syntaxcode code;
  292.   Lisp_Object table = buf->syntax_table;
  293.   
  294.   while (count > 0)
  295.     {
  296.       QUIT;
  297.   
  298.       while (1)
  299.     {
  300.       if (from == end)
  301.         {
  302.           return 0;
  303.         }
  304.       code = SYNTAX (table, BUF_FETCH_CHAR (buf, from));
  305.       if (words_include_escapes
  306.           && (code == Sescape || code == Scharquote))
  307.         break;
  308.       if (code == Sword)
  309.         break;
  310.       from++;
  311.     }
  312.  
  313.       QUIT;
  314.  
  315.       while (1)
  316.     {
  317.       if (from == end) break;
  318.       code = SYNTAX (table, BUF_FETCH_CHAR (buf, from));
  319.       if (!(words_include_escapes
  320.         && (code == Sescape || code == Scharquote)))
  321.         if (code != Sword)
  322.           break;
  323.       from++;
  324.     }
  325.  
  326.       count--;
  327.     }
  328.   while (count < 0)
  329.     {
  330.       QUIT;
  331.  
  332.       while (1)
  333.     {
  334.       if (from == beg)
  335.         {
  336.           return 0;
  337.         }
  338.       code = SYNTAX (table, BUF_FETCH_CHAR (buf, from - 1));
  339.       if (words_include_escapes
  340.           && (code == Sescape || code == Scharquote))
  341.         break;
  342.       if (code == Sword)
  343.         break;
  344.       from--;
  345.     }
  346.  
  347.       QUIT;
  348.  
  349.       while (1)
  350.     {
  351.       if (from == beg) break;
  352.       code = SYNTAX (table, BUF_FETCH_CHAR (buf, from - 1));
  353.       if (!(words_include_escapes
  354.         && (code == Sescape || code == Scharquote)))
  355.         if (code != Sword)
  356.           break;
  357.       from--;
  358.     }
  359.       count++;
  360.     }
  361.  
  362.   return from;
  363. }
  364.  
  365. DEFUN ("forward-word", Fforward_word, Sforward_word, 1, 2, "_p",
  366.   "Move point forward ARG words (backward if ARG is negative).\n\
  367. Normally returns t.\n\
  368. If an edge of the buffer is reached, point is left there\n\
  369. and nil is returned.")
  370.   (count, buffer)
  371.      Lisp_Object count, buffer;
  372. {
  373.   Bufpos val;
  374.   struct buffer *buf = decode_buffer (buffer, 0);
  375.   CHECK_INT (count, 0);
  376.  
  377.   if (!(val = scan_words (buf, BUF_PT (buf), XINT (count))))
  378.     {
  379.       BUF_SET_PT (buf, XINT (count) > 0 ? BUF_ZV (buf) : BUF_BEGV (buf));
  380.       return Qnil;
  381.     }
  382.   BUF_SET_PT (buf, val);
  383.   return Qt;
  384. }
  385.  
  386. static void scan_sexps_forward (struct buffer *buf,
  387.                 struct lisp_parse_state *,
  388.                 int from, int end, 
  389.                 int targetdepth, int stopbefore,
  390.                 Lisp_Object oldstate,
  391.                 int commentstop);
  392.  
  393. static int
  394. find_start_of_comment (struct buffer *buf, int from, int stop, int mask)
  395. {
  396.   int c;
  397.   enum syntaxcode code;
  398.   Lisp_Object table = buf->syntax_table;
  399.  
  400.   /* Look back, counting the parity of string-quotes,
  401.      and recording the comment-starters seen.
  402.      When we reach a safe place, assume that's not in a string;
  403.      then step the main scan to the earliest comment-starter seen
  404.      an even number of string quotes away from the safe place.
  405.      
  406.      OFROM[I] is position of the earliest comment-starter seen
  407.      which is I+2X quotes from the comment-end.
  408.      PARITY is current parity of quotes from the comment end.  */
  409.   int parity = 0;
  410.   char my_stringend = 0;
  411.   int string_lossage = 0;
  412.   int comment_end = from;
  413.   int comstart_pos = 0;
  414.   int comstart_parity = 0;
  415.   int styles_match_p = 0;
  416.  
  417.   /* At beginning of range to scan, we're outside of strings;
  418.      that determines quote parity to the comment-end.  */
  419.   while (from != stop)
  420.     {
  421.       /* Move back and examine a character.  */
  422.       from--;
  423.  
  424.       c = BUF_FETCH_CHAR (buf, from);
  425.       code = SYNTAX (table, c);
  426.  
  427.       /* is this a 1-char comment end sequence? if so, try
  428.      to see if style matches previously extracted mask */
  429.       if (code == Sendcomment)
  430.     {
  431.       styles_match_p = SYNTAX_STYLES_MATCH_1CHAR_P (table, c, mask);
  432.     }
  433.  
  434.       /* otherwise, is this a 2-char comment end sequence? */
  435.       else if (from >= stop
  436.            && SYNTAX_END_P (table, c, BUF_FETCH_CHAR (buf, from+1)))
  437.     {
  438.       code = Sendcomment;
  439.       styles_match_p = SYNTAX_STYLES_MATCH_END_P (table, c,
  440.                               BUF_FETCH_CHAR (buf, from+1),
  441.                               mask);
  442.     }
  443.             
  444.       /* or are we looking at a 1-char comment start sequence
  445.      of the style matching mask? */
  446.       else if (code == Scomment
  447.            && SYNTAX_STYLES_MATCH_1CHAR_P (table, c, mask))
  448.     {
  449.       styles_match_p = 1;
  450.     }
  451.             
  452.       /* or possibly, a 2-char comment start sequence */
  453.       else if (from >= stop
  454.            && SYNTAX_STYLES_MATCH_START_P (table, c, BUF_FETCH_CHAR (buf, from+1),
  455.                            mask))
  456.     {
  457.       code = Scomment;
  458.       styles_match_p = 1;
  459.     }
  460.  
  461.       /* Ignore escaped characters.  */
  462.       if (char_quoted (buf, from))
  463.     continue;
  464.  
  465.       /* Track parity of quotes.  */
  466.       if (code == Sstring)
  467.     {
  468.       parity ^= 1;
  469.       if (my_stringend == 0)
  470.         my_stringend = c;
  471.       /* If we have two kinds of string delimiters.
  472.          There's no way to grok this scanning backwards.  */
  473.       else if (my_stringend != c)
  474.         string_lossage = 1;
  475.     }
  476.  
  477.       /* Record comment-starters according to that
  478.      quote-parity to the comment-end.  */
  479.       if (code == Scomment && styles_match_p)
  480.     {
  481.       comstart_parity = parity;
  482.       comstart_pos = from;
  483.     }
  484.  
  485.       /* If we find another earlier comment-ender,
  486.      any comment-starts earlier than that don't count
  487.      (because they go with the earlier comment-ender).  */
  488.       if (code == Sendcomment && styles_match_p)
  489.     break;
  490.  
  491.       /* Assume a defun-start point is outside of strings.  */
  492.       if (code == Sopen
  493.       && (from == stop || BUF_FETCH_CHAR (buf, from - 1) == '\n'))
  494.     break;
  495.     }
  496.  
  497.   if (comstart_pos == 0)
  498.     from = comment_end;
  499.   /* If the earliest comment starter
  500.      is followed by uniform paired string quotes or none,
  501.      we know it can't be inside a string
  502.      since if it were then the comment ender would be inside one.
  503.      So it does start a comment.  Skip back to it.  */
  504.   else if (comstart_parity == 0 && !string_lossage)
  505.     from = comstart_pos;
  506.   else
  507.     {
  508.       /* We had two kinds of string delimiters mixed up
  509.      together.  Decode this going forwards.
  510.      Scan fwd from the previous comment ender
  511.      to the one in question; this records where we
  512.      last passed a comment starter.  */
  513.  
  514.       struct lisp_parse_state state;
  515.       scan_sexps_forward (buf, &state, find_defun_start (buf, comment_end),
  516.               comment_end - 1, -10000, 0, Qnil, 0);
  517.       if (state.incomment)
  518.     from = state.comstart;
  519.       else
  520.     /* We can't grok this as a comment; scan it normally.  */
  521.     from = comment_end;
  522.     }
  523.   return from;
  524. }
  525.  
  526. static int
  527. find_end_of_comment (struct buffer *buf, int from, int stop, int mask)
  528. {
  529.   int c;
  530.   Lisp_Object table = buf->syntax_table;
  531.  
  532.   while (1)
  533.     {
  534.       if (from == stop)
  535.     {
  536.       return -1;
  537.     }
  538.       c = BUF_FETCH_CHAR (buf, from);
  539.       if (SYNTAX (table, c) == Sendcomment
  540.       && SYNTAX_STYLES_MATCH_1CHAR_P (table, c, mask))
  541.     /* we have encountered a comment end of the same style
  542.        as the comment sequence which began this comment
  543.        section */
  544.     break;
  545.  
  546.       from++;
  547.       if (from < stop
  548.       && SYNTAX_STYLES_MATCH_END_P (table, c, BUF_FETCH_CHAR (buf, from), mask))
  549.     /* we have encountered a comment end of the same style
  550.        as the comment sequence which began this comment
  551.        section */
  552.     { from++; break; }
  553.     }
  554.   return from;
  555. }
  556.  
  557.  
  558. /* #### between FSF 19.23 and 19.28 there are some changes to the logic
  559.    in this function (and minor changes to find_start_of_comment(),
  560.    above, which is part of Fforward_comment() in FSF).  Attempts to port
  561.    that logic made this function break, so I'm leaving it out.  If anyone
  562.    ever complains about this function not working properly, take a look
  563.    at those changes.  --ben */
  564.  
  565. DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 2, 0,
  566.   "Move forward across up to N comments.  If N is negative, move backward.\n\
  567. Stop scanning if we find something other than a comment or whitespace.\n\
  568. Set point to where scanning stops.\n\
  569. If N comments are found as expected, with nothing except whitespace\n\
  570. between them, return t; otherwise return nil.\n\
  571. Point is set in either case.\n\
  572. Optional argument BUFFER defaults to the current buffer.")
  573.      (n, buffer)
  574.      Lisp_Object n, buffer;
  575. {
  576.   int from;
  577.   int stop;
  578.   int c;
  579.   enum syntaxcode code;
  580.   int count;
  581.   struct buffer *buf = decode_buffer (buffer, 0);
  582.   Lisp_Object table = buf->syntax_table;
  583.  
  584.   CHECK_INT (n, 0);
  585.   count = XINT (n);
  586.  
  587.   from = BUF_PT (buf);
  588.  
  589.   while (count > 0)
  590.     {
  591.       QUIT;
  592.  
  593.       stop = BUF_ZV (buf);
  594.       while (from < stop)
  595.     {
  596.       int mask = 0;         /* mask for finding matching comment style */
  597.  
  598.       if (char_quoted (buf, from))
  599.         {
  600.           from++;
  601.           continue;
  602.         }
  603.  
  604.       c = BUF_FETCH_CHAR (buf, from);
  605.       code = SYNTAX (table, c);
  606.  
  607.       if (code == Scomment)
  608.         {
  609.           /* we have encountered a single character comment start
  610.          sequence, and we are ignoring all text inside comments.
  611.          we must record the comment style this character begins
  612.          so that later, only a comment end of the same style actually
  613.          ends the comment section */
  614.           mask = SYNTAX_COMMENT_1CHAR_MASK (table, c);
  615.         }
  616.  
  617.       else if (from < stop
  618.            && SYNTAX_START_P (table, c, BUF_FETCH_CHAR (buf, from+1)))
  619.         {
  620.           /* we have encountered a 2char comment start sequence and we 
  621.          are ignoring all text inside comments. we must record
  622.          the comment style this sequence begins so that later,
  623.          only a comment end of the same style actually ends
  624.          the comment section */
  625.           code = Scomment;
  626.           mask = SYNTAX_COMMENT_MASK_START (table, c,
  627.                         BUF_FETCH_CHAR (buf, from+1));
  628.           from++;
  629.         }
  630.  
  631.       if (code == Scomment)
  632.         {
  633.           int newfrom;
  634.  
  635.           newfrom = find_end_of_comment (buf, from, stop, mask);
  636.           if (newfrom < 0)
  637.         {
  638.           /* we stopped because from==stop */
  639.           BUF_SET_PT (buf, stop);
  640.           return Qnil;
  641.         }
  642.           from = newfrom;
  643.  
  644.           /* We have skipped one comment.  */
  645.           break;
  646.         }
  647.       else if (code != Swhitespace
  648.            && code != Sendcomment
  649.            && code != Scomment )
  650.         {
  651.           BUF_SET_PT (buf, from);
  652.           return Qnil;
  653.         }
  654.       from++;
  655.     }
  656.  
  657.       /* End of comment reached */
  658.       count--;
  659.     }
  660.  
  661.   while (count < 0)
  662.     {
  663.       QUIT;
  664.  
  665.       stop = BUF_BEGV (buf);
  666.       while (from > stop)
  667.     {
  668.           int mask = 0;         /* mask for finding matching comment style */
  669.  
  670.       from--;
  671.       if (char_quoted (buf, from))
  672.         {
  673.           from--;
  674.           continue;
  675.         }
  676.           
  677.       c = BUF_FETCH_CHAR (buf, from);
  678.       code = SYNTAX (table, c);
  679.  
  680.       if (code == Sendcomment)
  681.         {
  682.           /* we have found a single char end comment. we must record
  683.          the comment style encountered so that later, we can match
  684.          only the proper comment begin sequence of the same style */
  685.           mask = SYNTAX_COMMENT_1CHAR_MASK (table, c);
  686.         }
  687.  
  688.       else if (from > stop
  689.            && SYNTAX_END_P (table, BUF_FETCH_CHAR (buf, from - 1), c)
  690.            && !char_quoted (buf, from - 1))
  691.         {
  692.           /* We must record the comment style encountered so that
  693.          later, we can match only the proper comment begin
  694.          sequence of the same style.  */
  695.           code = Sendcomment;
  696.           mask = SYNTAX_COMMENT_MASK_END (BUF_FETCH_CHAR (buf, from - 1), c);
  697.           from--;
  698.         }
  699.  
  700.       if (code == Sendcomment)
  701.          {
  702.            from = find_start_of_comment (buf, from, stop, mask);
  703.            break;
  704.             }
  705.  
  706.       else if (code != Swhitespace
  707.            && SYNTAX (table, c) != Scomment
  708.            && SYNTAX (table, c) != Sendcomment)
  709.         {
  710.           BUF_SET_PT (buf, from + 1);
  711.           return Qnil;
  712.         }
  713.     }
  714.  
  715.       count++;
  716.     }
  717.  
  718.   BUF_SET_PT (buf, from);
  719.   return Qt;
  720. }
  721.  
  722.  
  723. Lisp_Object
  724. scan_lists (struct buffer *buf, int from, int count, int depth, int sexpflag,
  725.         int no_error)
  726. {
  727.   int stop;
  728.   int c;
  729.   int quoted;
  730.   int mathexit = 0;
  731.   enum syntaxcode code;
  732.   int min_depth = depth;    /* Err out if depth gets less than this. */
  733.   Lisp_Object table = buf->syntax_table;
  734.  
  735.   if (depth > 0) min_depth = 0;
  736.  
  737.   while (count > 0)
  738.     {
  739.       QUIT;
  740.  
  741.       stop = BUF_ZV (buf);
  742.       while (from < stop)
  743.     {
  744.           int mask = 0;         /* mask for finding matching comment style */
  745.  
  746.       c = BUF_FETCH_CHAR (buf, from);
  747.       code = SYNTAX (table, c);
  748.       from++;
  749.  
  750.       /* a 1-char comment start sequence */
  751.       if (code == Scomment && parse_sexp_ignore_comments)
  752.         {
  753.           mask = SYNTAX_COMMENT_1CHAR_MASK (table, c);
  754.         }
  755.  
  756.       /* else, a 2-char comment start sequence? */
  757.       else if (from < stop
  758.            && SYNTAX_START_P (table, c, BUF_FETCH_CHAR (buf, from))
  759.            && parse_sexp_ignore_comments)
  760.         {
  761.           /* we have encountered a comment start sequence and we 
  762.          are ignoring all text inside comments. we must record
  763.          the comment style this sequence begins so that later,
  764.          only a comment end of the same style actually ends
  765.          the comment section */
  766.           code = Scomment;
  767.           mask = SYNTAX_COMMENT_MASK_START (table, c, BUF_FETCH_CHAR (buf, from));
  768.           from++;
  769.         }
  770.       
  771.       if (SYNTAX_PREFIX (table, c))
  772.         continue;
  773.  
  774.       switch (code)
  775.         {
  776.         case Sescape:
  777.         case Scharquote:
  778.           if (from == stop) goto lose;
  779.           from++;
  780.           /* treat following character as a word constituent */
  781.         case Sword:
  782.         case Ssymbol:
  783.           if (depth || !sexpflag) break;
  784.           /* This word counts as a sexp; return at end of it. */
  785.           while (from < stop)
  786.         {
  787.           switch (SYNTAX (table, BUF_FETCH_CHAR (buf, from)))
  788.             {
  789.             case Scharquote:
  790.             case Sescape:
  791.               from++;
  792.               if (from == stop) goto lose;
  793.               break;
  794.             case Sword:
  795.             case Ssymbol:
  796.             case Squote:
  797.               break;
  798.             default:
  799.               goto done;
  800.             }
  801.           from++;
  802.         }
  803.           goto done;
  804.  
  805.         case Scomment:
  806.           if (!parse_sexp_ignore_comments)
  807.         break;
  808.           {
  809.         int newfrom = find_end_of_comment (buf, from, stop, mask);
  810.         if (newfrom < 0)
  811.           {
  812.             /* we stopped because from == stop in search forward */
  813.             from = stop;
  814.             if (depth == 0)
  815.               goto done;
  816.             goto lose;
  817.           }
  818.         from = newfrom;
  819.           }
  820.           break;
  821.  
  822.         case Smath:
  823.           if (!sexpflag)
  824.         break;
  825.           if (from != stop && c == BUF_FETCH_CHAR (buf, from))
  826.         from++;
  827.           if (mathexit)
  828.         {
  829.           mathexit = 0;
  830.           goto close1;
  831.         }
  832.           mathexit = 1;
  833.  
  834.         case Sopen:
  835.           if (!++depth) goto done;
  836.           break;
  837.  
  838.         case Sclose:
  839.         close1:
  840.           if (!--depth) goto done;
  841.           if (depth < min_depth)
  842.         {
  843.           if (no_error)
  844.             return Qnil;
  845.           error ("Containing expression ends prematurely");
  846.         }
  847.           break;
  848.  
  849.         case Sstring:
  850.               {
  851.         /* XEmacs change: call SYNTAX_MATCH on character */
  852.                 int ch = BUF_FETCH_CHAR (buf, from - 1);
  853.                 unsigned char stringterm = SYNTAX_MATCH (table, ch);
  854.                 if (stringterm == 0)
  855.                   stringterm = ch;
  856.               
  857.                 while (1)
  858.         {
  859.           if (from >= stop)
  860.             goto lose;
  861.           if (BUF_FETCH_CHAR (buf, from) == stringterm)
  862.             break;
  863.           switch (SYNTAX (table, BUF_FETCH_CHAR (buf, from)))
  864.                   {
  865.                   case Scharquote:
  866.                   case Sescape:
  867.                     from++;
  868.                     break;
  869.                   default:
  870.                     break;
  871.                   }
  872.           from++;
  873.         }
  874.                 from++;
  875.                 if (!depth && sexpflag) goto done;
  876.                 break;
  877.               }
  878.  
  879.             default:
  880.               break;
  881.         }
  882.     }
  883.  
  884.       /* Reached end of buffer.  Error if within object,
  885.      return nil if between */
  886.       if (depth) goto lose;
  887.  
  888.       return Qnil;
  889.  
  890.       /* End of object reached */
  891.     done:
  892.       count--;
  893.     }
  894.  
  895.   while (count < 0)
  896.     {
  897.       QUIT;
  898.  
  899.       stop = BUF_BEGV (buf);
  900.       while (from > stop)
  901.     {
  902.           int mask = 0;         /* mask for finding matching comment style */
  903.  
  904.       from--;
  905.           quoted = char_quoted (buf, from);
  906.       if (quoted)
  907.         from--;
  908.  
  909.       c = BUF_FETCH_CHAR (buf, from);
  910.       code = SYNTAX (table, c);
  911.  
  912.       if (code == Sendcomment && parse_sexp_ignore_comments)
  913.         {
  914.           /* we have found a single char end comment. we must record
  915.          the comment style encountered so that later, we can match
  916.          only the proper comment begin sequence of the same style */
  917.           mask = SYNTAX_COMMENT_1CHAR_MASK (table, c);
  918.         }
  919.  
  920.       else if (from > stop
  921.            && SYNTAX_END_P (table, BUF_FETCH_CHAR (buf, from-1), c)
  922.            && !char_quoted (buf, from - 1)
  923.            && parse_sexp_ignore_comments)
  924.         {
  925.           /* we must record the comment style encountered so that
  926.          later, we can match only the proper comment begin
  927.          sequence of the same style */
  928.           code = Sendcomment;
  929.           mask = SYNTAX_COMMENT_MASK_END (BUF_FETCH_CHAR (buf, from - 1),
  930.                           c);
  931.           from--;
  932.         }
  933.  
  934.       if (SYNTAX_PREFIX (table, c))
  935.         continue;
  936.  
  937.       switch (((quoted) ? Sword : code))
  938.         {
  939.         case Sword:
  940.         case Ssymbol:
  941.           if (depth || !sexpflag) break;
  942.           /* This word counts as a sexp; count object finished after 
  943.          passing it. */
  944.           while (from > stop)
  945.         {
  946.           quoted = char_quoted (buf, from - 1);
  947.           if (quoted)
  948.             from--;
  949.           if (! (quoted
  950.                          || SYNTAX (table,
  951.                     BUF_FETCH_CHAR (buf, from - 1)) == Sword
  952.              || SYNTAX (table,
  953.                     BUF_FETCH_CHAR (buf, from - 1)) == Ssymbol
  954.              || SYNTAX (table,
  955.                     BUF_FETCH_CHAR (buf, from - 1)) == Squote))
  956.                     goto done2;
  957.           from--;
  958.         }
  959.           goto done2;
  960.  
  961.         case Smath:
  962.           if (!sexpflag)
  963.         break;
  964.           if (from != stop && c == BUF_FETCH_CHAR (buf, from - 1))
  965.         from--;
  966.           if (mathexit)
  967.         {
  968.           mathexit = 0;
  969.           goto open2;
  970.         }
  971.           mathexit = 1;
  972.  
  973.         case Sclose:
  974.           if (!++depth) goto done2;
  975.           break;
  976.  
  977.         case Sopen:
  978.         open2:
  979.           if (!--depth) goto done2;
  980.           if (depth < min_depth)
  981.         {
  982.           if (no_error)
  983.             return Qnil;
  984.           error ("Containing expression ends prematurely");
  985.         }
  986.           break;
  987.  
  988.         case Sendcomment:
  989.           if (parse_sexp_ignore_comments)
  990.         from = find_start_of_comment (buf, from, stop, mask);
  991.           break;
  992.  
  993.         case Sstring:
  994.               {
  995.         /* XEmacs change: call SYNTAX_MATCH on character */
  996.                 int ch = BUF_FETCH_CHAR (buf, from);
  997.                 unsigned char stringterm = SYNTAX_MATCH (table, ch);
  998.                 if (stringterm == 0)
  999.                   stringterm = ch;
  1000.  
  1001.                 while (1)
  1002.         {
  1003.           if (from == stop) goto lose;
  1004.           if (!char_quoted (buf, from - 1)
  1005.                && stringterm == BUF_FETCH_CHAR (buf, from - 1))
  1006.             break;
  1007.           from--;
  1008.         }
  1009.                 from--;
  1010.                 if (!depth && sexpflag) goto done2;
  1011.                 break;
  1012.               }
  1013.             }
  1014.     }
  1015.  
  1016.       /* Reached start of buffer.  Error if within object,
  1017.      return nil if between */
  1018.       if (depth) goto lose;
  1019.  
  1020.       return Qnil;
  1021.  
  1022.     done2:
  1023.       count++;
  1024.     }
  1025.  
  1026.  
  1027.   return (make_number (from));
  1028.  
  1029.  lose:
  1030.   if (!no_error)
  1031.     error ("Unbalanced parentheses");
  1032.   return Qnil;
  1033. }
  1034.  
  1035. int
  1036. char_quoted (struct buffer *buf, int pos)
  1037. {
  1038.   enum syntaxcode code;
  1039.   int beg = BUF_BEGV (buf);
  1040.   int quoted = 0;
  1041.   Lisp_Object table = buf->syntax_table;
  1042.  
  1043.   while (pos > beg
  1044.      && ((code = SYNTAX (table, BUF_FETCH_CHAR (buf, pos - 1))) == Scharquote
  1045.          || code == Sescape))
  1046.     pos--, quoted = !quoted;
  1047.   return quoted;
  1048. }
  1049.  
  1050. DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 5, 0,
  1051.   "Scan from character number FROM by COUNT lists.\n\
  1052. Returns the character number of the position thus found.\n\
  1053. \n\
  1054. If DEPTH is nonzero, paren depth begins counting from that value,\n\
  1055. only places where the depth in parentheses becomes zero\n\
  1056. are candidates for stopping; COUNT such places are counted.\n\
  1057. Thus, a positive value for DEPTH means go out levels.\n\
  1058. \n\
  1059. Comments are ignored if `parse-sexp-ignore-comments' is non-nil.\n\
  1060. \n\
  1061. If the beginning or end of (the accessible part of) the buffer is reached\n\
  1062. and the depth is wrong, an error is signaled.\n\
  1063. If the depth is right but the count is not used up, nil is returned.\n\
  1064. \n\
  1065. If optional arg BUFFER is non-nil, scanning occurs in that buffer instead\n\
  1066. of in the current buffer.\n\
  1067. \n\
  1068. If optional arg NOERROR is non-nil, scan-lists will return nil instead of\n\
  1069. signalling an error.")
  1070.   (from, count, depth, buffer, no_error)
  1071.      Lisp_Object from, count, depth, buffer, no_error;
  1072. {
  1073.   struct buffer *buf;
  1074.  
  1075.   CHECK_INT (from, 0);
  1076.   CHECK_INT (count, 1);
  1077.   CHECK_INT (depth, 2);
  1078.   buf = decode_buffer (buffer, 0);
  1079.  
  1080.   return scan_lists (buf, XINT (from), XINT (count), XINT (depth), 0,
  1081.              !NILP (no_error));
  1082. }
  1083.  
  1084. DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 4, 0,
  1085.   "Scan from character number FROM by COUNT balanced expressions.\n\
  1086. If COUNT is negative, scan backwards.\n\
  1087. Returns the character number of the position thus found.\n\
  1088. \n\
  1089. Comments are ignored if `parse-sexp-ignore-comments' is non-nil.\n\
  1090. \n\
  1091. If the beginning or end of (the accessible part of) the buffer is reached\n\
  1092. in the middle of a parenthetical grouping, an error is signaled.\n\
  1093. If the beginning or end is reached between groupings\n\
  1094. but before count is used up, nil is returned.\n\
  1095. \n\
  1096. If optional arg BUFFER is non-nil, scanning occurs in that buffer instead\n\
  1097. of in the current buffer.\n\
  1098. \n\
  1099. If optional arg NOERROR is non-nil, scan-sexps will return nil instead of\n\
  1100. signalling an error.")
  1101.   (from, count, buffer, no_error)
  1102.      Lisp_Object from, count, buffer, no_error;
  1103. {
  1104.   struct buffer *buf = decode_buffer (buffer, 0);
  1105.   CHECK_INT (from, 0);
  1106.   CHECK_INT (count, 1);
  1107.  
  1108.   return scan_lists (buf, XINT (from), XINT (count), 0, 1, !NILP (no_error));
  1109. }
  1110.  
  1111. DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
  1112.   0, 1, 0,
  1113.   "Move point backward over any number of chars with prefix syntax.\n\
  1114. This includes chars with \"quote\" or \"prefix\" syntax (' or p).\n\
  1115. \n\
  1116. Optional arg BUFFER defaults to the current buffer.")
  1117.   (buffer)
  1118.   Lisp_Object buffer;
  1119. {
  1120.   struct buffer *buf = decode_buffer (buffer, 0);
  1121.   int beg = BUF_BEGV (buf);
  1122.   int pos = BUF_PT (buf);
  1123.   Lisp_Object table = buf->syntax_table;
  1124.  
  1125.   while (pos > beg && !char_quoted (buf, pos - 1)
  1126.      && (SYNTAX (table, BUF_FETCH_CHAR (buf, pos - 1)) == Squote
  1127.          || SYNTAX_PREFIX (table, BUF_FETCH_CHAR (buf, pos - 1))))
  1128.     pos--;
  1129.  
  1130.   BUF_SET_PT (buf, pos);
  1131.  
  1132.   return Qnil;
  1133. }
  1134.  
  1135. /* Parse forward from FROM to END,
  1136.    assuming that FROM has state OLDSTATE (nil means FROM is start of function),
  1137.    and return a description of the state of the parse at END.
  1138.    If STOPBEFORE is nonzero, stop at the start of an atom.
  1139.    If COMMENTSTOP is nonzero, stop at the start of a comment.  */
  1140.  
  1141. static void
  1142. scan_sexps_forward (struct buffer *buf, struct lisp_parse_state *stateptr,
  1143.             Bufpos from, Bufpos end, 
  1144.             int targetdepth, int stopbefore,
  1145.             Lisp_Object oldstate,
  1146.             int commentstop)
  1147. {
  1148.   struct lisp_parse_state state;
  1149.  
  1150.   enum syntaxcode code;
  1151.   struct level { int last, prev; };
  1152.   struct level levelstart[100];
  1153.   struct level *curlevel = levelstart;
  1154.   struct level *endlevel = levelstart + 100;
  1155.   int depth;    /* Paren depth of current scanning location.
  1156.                level - levelstart equals this except
  1157.                when the depth becomes negative.  */
  1158.   int mindepth;        /* Lowest DEPTH value seen.  */
  1159.   int start_quoted = 0;        /* Nonzero means starting after a char quote */
  1160.   Lisp_Object table = buf->syntax_table;
  1161.   Lisp_Object tem;
  1162.   int mask;                     /* comment mask */
  1163.  
  1164.   if (NILP (oldstate))
  1165.     {
  1166.       depth = 0;
  1167.       state.instring = -1;
  1168.       state.incomment = 0;
  1169.       state.comstyle = 0;    /* comment style a by default */
  1170.       mask = SYNTAX_COMMENT_STYLE_A;
  1171.     }
  1172.   else
  1173.     {
  1174.       tem = Fcar (oldstate);    /* elt 0, depth */
  1175.       if (!NILP (tem))
  1176.     depth = XINT (tem);
  1177.       else
  1178.     depth = 0;
  1179.  
  1180.       oldstate = Fcdr (oldstate);
  1181.       oldstate = Fcdr (oldstate);
  1182.       oldstate = Fcdr (oldstate);
  1183.       tem = Fcar (oldstate);    /* elt 3, instring */
  1184.       state.instring = !NILP (tem) ? XINT (tem) : -1;
  1185.  
  1186.       oldstate = Fcdr (oldstate); /* elt 4, incomment */
  1187.       tem = Fcar (oldstate);
  1188.       state.incomment = !NILP (tem);
  1189.  
  1190.       oldstate = Fcdr (oldstate);
  1191.       tem = Fcar (oldstate);    /* elt 5, follows-quote */
  1192.       start_quoted = !NILP (tem);
  1193.  
  1194.       /* if the eighth element of the list is nil, we are in comment style
  1195.      a. if it is non-nil, we are in comment style b */
  1196.       oldstate = Fcdr (oldstate);
  1197.       oldstate = Fcdr (oldstate);
  1198.       oldstate = Fcdr (oldstate);
  1199.       tem = Fcar (oldstate);    /* elt 8, comment style a */
  1200.       state.comstyle = !NILP (tem);
  1201.       mask = state.comstyle ? SYNTAX_COMMENT_STYLE_B : SYNTAX_COMMENT_STYLE_A;
  1202.     }
  1203.   state.quoted = 0;
  1204.   mindepth = depth;
  1205.  
  1206.   curlevel->prev = -1;
  1207.   curlevel->last = -1;
  1208.  
  1209.   /* Enter the loop at a place appropriate for initial state. */
  1210.  
  1211.   if (state.incomment) goto startincomment;
  1212.   if (state.instring >= 0)
  1213.     {
  1214.       if (start_quoted) goto startquotedinstring;
  1215.       goto startinstring;
  1216.     }
  1217.   if (start_quoted) goto startquoted;
  1218.  
  1219.   while (from < end)
  1220.     {
  1221.       QUIT;
  1222.  
  1223.       code = SYNTAX (table, BUF_FETCH_CHAR (buf, from));
  1224.       from++;
  1225.  
  1226.       if (code == Scomment)
  1227.     {
  1228.       /* record the comment style we have entered so that only the
  1229.          comment-ender sequence (or single char) of the same style
  1230.          actually terminates the comment section. */
  1231.       mask = SYNTAX_COMMENT_1CHAR_MASK (table,
  1232.                         BUF_FETCH_CHAR (buf, from-1));
  1233.       state.comstyle = (mask == SYNTAX_COMMENT_STYLE_B);
  1234.       state.comstart = from - 1;
  1235.     }
  1236.       
  1237.       else if (from < end &&
  1238.            SYNTAX_START_P (table, BUF_FETCH_CHAR (buf, from-1),
  1239.                    BUF_FETCH_CHAR (buf, from)))
  1240.     {
  1241.       /* Record the comment style we have entered so that only
  1242.          the comment-end sequence of the same style actually
  1243.          terminates the comment section.  */
  1244.       code = Scomment;
  1245.       mask = SYNTAX_COMMENT_MASK_START (table,
  1246.                                             BUF_FETCH_CHAR (buf, from-1),
  1247.                         BUF_FETCH_CHAR (buf, from));
  1248.       state.comstyle = (mask == SYNTAX_COMMENT_STYLE_B);
  1249.       state.comstart = from-1;
  1250.       from++;
  1251.     }
  1252.  
  1253.       if (SYNTAX_PREFIX (table, BUF_FETCH_CHAR (buf, from - 1)))
  1254.     continue;
  1255.       switch (code)
  1256.     {
  1257.     case Sescape:
  1258.     case Scharquote:
  1259.       if (stopbefore) goto stop;  /* this arg means stop at sexp start */
  1260.       curlevel->last = from - 1;
  1261.     startquoted:
  1262.       if (from == end) goto endquoted;
  1263.       from++;
  1264.       goto symstarted;
  1265.       /* treat following character as a word constituent */
  1266.     case Sword:
  1267.     case Ssymbol:
  1268.       if (stopbefore) goto stop;  /* this arg means stop at sexp start */
  1269.       curlevel->last = from - 1;
  1270.     symstarted:
  1271.       while (from < end)
  1272.         {
  1273.           switch (SYNTAX (table, BUF_FETCH_CHAR (buf, from)))
  1274.         {
  1275.         case Scharquote:
  1276.         case Sescape:
  1277.           from++;
  1278.           if (from == end) goto endquoted;
  1279.           break;
  1280.         case Sword:
  1281.         case Ssymbol:
  1282.         case Squote:
  1283.           break;
  1284.         default:
  1285.           goto symdone;
  1286.         }
  1287.           from++;
  1288.         }
  1289.     symdone:
  1290.       curlevel->prev = curlevel->last;
  1291.       break;
  1292.  
  1293.     case Scomment:
  1294.       state.incomment = 1;
  1295.     startincomment:
  1296.       if (commentstop)
  1297.         goto done;
  1298.       {
  1299.         int newfrom = find_end_of_comment (buf, from, end, mask);
  1300.         if (newfrom < 0)
  1301.           {
  1302.         /* we terminated search because from == end */
  1303.         from = end;
  1304.         goto done;
  1305.           }
  1306.         from = newfrom;
  1307.       }
  1308.       state.incomment = 0;
  1309.       state.comstyle = 0;             /* reset the comment style */
  1310.       mask = 0;
  1311.       break;
  1312.  
  1313.     case Sopen:
  1314.       if (stopbefore) goto stop;  /* this arg means stop at sexp start */
  1315.       depth++;
  1316.       /* curlevel++->last ran into compiler bug on Apollo */
  1317.       curlevel->last = from - 1;
  1318.       if (++curlevel == endlevel)
  1319.         error ("Nesting too deep for parser");
  1320.       curlevel->prev = -1;
  1321.       curlevel->last = -1;
  1322.       if (!--targetdepth) goto done;
  1323.       break;
  1324.  
  1325.     case Sclose:
  1326.       depth--;
  1327.       if (depth < mindepth)
  1328.         mindepth = depth;
  1329.       if (curlevel != levelstart)
  1330.         curlevel--;
  1331.       curlevel->prev = curlevel->last;
  1332.       if (!++targetdepth) goto done;
  1333.       break;
  1334.  
  1335.     case Sstring:
  1336.           {
  1337.             Emchar ch;
  1338.             if (stopbefore) goto stop; /* this arg means stop at sexp start */
  1339.             curlevel->last = from - 1;
  1340.         /* XEmacs change: call SYNTAX_MATCH on character */
  1341.             ch = BUF_FETCH_CHAR (buf, from - 1);
  1342.             state.instring = SYNTAX_MATCH (table, ch);
  1343.             if (state.instring == 0) state.instring = ch;
  1344.           }
  1345.     startinstring:
  1346.       while (1)
  1347.         {
  1348.           if (from >= end) goto done;
  1349.           if (BUF_FETCH_CHAR (buf, from) == state.instring) break;
  1350.           switch (SYNTAX (table, BUF_FETCH_CHAR (buf, from)))
  1351.         {
  1352.         case Scharquote:
  1353.         case Sescape:
  1354.                   {
  1355.                     from++;
  1356.                   startquotedinstring:
  1357.                     if (from >= end) goto endquoted;
  1358.                     break;
  1359.                   }
  1360.                 default:
  1361.                   break;
  1362.         }
  1363.           from++;
  1364.         }
  1365.       state.instring = -1;
  1366.       curlevel->prev = curlevel->last;
  1367.       from++;
  1368.       break;
  1369.  
  1370.     case Smath:
  1371.       break;
  1372.  
  1373.         case Swhitespace:
  1374.         case Spunct:
  1375.         case Squote:
  1376.         case Sendcomment:
  1377.     case Sinherit:
  1378.         case Smax:
  1379.           break;
  1380.     }
  1381.     }
  1382.   goto done;
  1383.  
  1384.  stop:   /* Here if stopping before start of sexp. */
  1385.   from--;    /* We have just fetched the char that starts it; */
  1386.   goto done; /* but return the position before it. */
  1387.  
  1388.  endquoted:
  1389.   state.quoted = 1;
  1390.  done:
  1391.   state.depth = depth;
  1392.   state.mindepth = mindepth;
  1393.   state.thislevelstart = curlevel->prev;
  1394.   state.prevlevelstart
  1395.     = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
  1396.   state.location = from;
  1397.  
  1398.   *stateptr = state;
  1399. }
  1400.  
  1401. DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 7, 0,
  1402.   "Parse Lisp syntax starting at FROM until TO; return status of parse at TO.\n\
  1403. Parsing stops at TO or when certain criteria are met;\n\
  1404.  point is set to where parsing stops.\n\
  1405. If fifth arg STATE is omitted or nil,\n\
  1406.  parsing assumes that FROM is the beginning of a function.\n\
  1407. Value is a list of eight elements describing final state of parsing:\n\
  1408.  0. depth in parens.\n\
  1409.  1. character address of start of innermost containing list; nil if none.\n\
  1410.  2. character address of start of last complete sexp terminated.\n\
  1411.  3. non-nil if inside a string.\n\
  1412.     (It is the character that will terminate the string.)\n\
  1413.  4. t if inside a comment.\n\
  1414.  5. t if following a quote character.\n\
  1415.  6. the minimum paren-depth encountered during this scan.\n\
  1416.  7. nil if in comment style a, or not in a comment; t if in comment style b\n\
  1417. If third arg TARGETDEPTH is non-nil, parsing stops if the depth\n\
  1418. in parentheses becomes equal to TARGETDEPTH.\n\
  1419. Fourth arg STOPBEFORE non-nil means stop when come to\n\
  1420.  any character that starts a sexp.\n\
  1421. Fifth arg STATE is an eight-element list like what this function returns.\n\
  1422. It is used to initialize the state of the parse.  Its second and third\n\
  1423. elements are ignored.\n\
  1424. Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.")
  1425.   (from, to, targetdepth, stopbefore, oldstate, commentstop, buffer)
  1426.      Lisp_Object from, to, targetdepth, stopbefore, oldstate, commentstop, buffer;
  1427. {
  1428.   struct lisp_parse_state state;
  1429.   int target;
  1430.   Bufpos start, end;
  1431.   struct buffer *buf = decode_buffer (buffer, 0);
  1432.  
  1433.   if (!NILP (targetdepth))
  1434.     {
  1435.       CHECK_INT (targetdepth, 3);
  1436.       target = XINT (targetdepth);
  1437.     }
  1438.   else
  1439.     target = -100000;        /* We won't reach this depth */
  1440.  
  1441.   get_bufrange (buf, from, to, &start, &end, 0);
  1442.   scan_sexps_forward (buf, &state, start, end,
  1443.               target, !NILP (stopbefore), oldstate,
  1444.               !NILP (commentstop));
  1445.  
  1446.   BUF_SET_PT (buf, state.location);
  1447.  
  1448.   {
  1449.     /*
  1450.      * This junk is necessary because of a bug in SparcWorks cc 2.0.1.  It
  1451.      * doesn't handle functions as arguments to other functions very well.
  1452.      */
  1453.     Lisp_Object retval[8];
  1454.  
  1455.     retval[0] = make_number (state.depth);
  1456.     retval[1] = ((state.prevlevelstart < 0) ? Qnil :
  1457.          make_number (state.prevlevelstart));
  1458.     retval[2] = ((state.thislevelstart < 0) ? Qnil :
  1459.          make_number (state.thislevelstart));
  1460.     retval[3] = ((state.instring >= 0) ? make_number (state.instring) : Qnil);
  1461.     retval[4] = ((state.incomment) ? Qt : Qnil);
  1462.     retval[5] = ((state.quoted) ? Qt : Qnil);
  1463.     retval[6] = make_number (state.mindepth);
  1464.     retval[7] = ((state.comstyle) ? Qt : Qnil);
  1465.  
  1466.     return (Flist (8, retval));
  1467.   }
  1468. }
  1469.  
  1470.  
  1471. /************************************************************************/
  1472. /*                            initialization                            */
  1473. /************************************************************************/
  1474.  
  1475. void
  1476. syms_of_syntax (void)
  1477. {
  1478.   defsymbol (&Qsyntax_table_p, "syntax-table-p");
  1479.  
  1480.   defsubr (&Ssyntax_table_p);
  1481.   defsubr (&Ssyntax_table);
  1482.   defsubr (&Sstandard_syntax_table);
  1483.   defsubr (&Scopy_syntax_table);
  1484.   defsubr (&Sset_syntax_table);
  1485.   defsubr (&Ssyntax_designator_chars);
  1486.   defsubr (&Schar_syntax);
  1487.   defsubr (&Smatching_paren);
  1488.   /* defsubr (&Smodify_syntax_entry); now in Lisp. */
  1489.   /* defsubr (&Sdescribe_syntax); now in Lisp. */
  1490.  
  1491.   defsubr (&Sforward_word);
  1492.  
  1493.   defsubr (&Sforward_comment);
  1494.   defsubr (&Sscan_lists);
  1495.   defsubr (&Sscan_sexps);
  1496.   defsubr (&Sbackward_prefix_chars);
  1497.   defsubr (&Sparse_partial_sexp);
  1498. }
  1499.  
  1500. void
  1501. vars_of_syntax (void)
  1502. {
  1503.   DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments,
  1504.     "Non-nil means `forward-sexp', etc., should treat comments as whitespace.");
  1505.  
  1506.   words_include_escapes = 0;
  1507.   DEFVAR_BOOL ("words-include-escapes", &words_include_escapes,
  1508.     "Non-nil means `forward-word', etc., should treat escape chars part of words.");
  1509.  
  1510.   no_quit_in_re_search = 0;
  1511. }
  1512.  
  1513. void
  1514. complex_vars_of_syntax (void)
  1515. {
  1516.   struct Lisp_Vector *v;
  1517.   int i;
  1518.  
  1519.   /* Set this now, so first buffer creation can refer to it. */
  1520.   /* Make it nil before calling copy-syntax-table
  1521.      so that copy-syntax-table will know not to try to copy from garbage */
  1522.   Vstandard_syntax_table = Qnil;
  1523.   Vstandard_syntax_table = Fcopy_syntax_table (Qnil);
  1524.   staticpro (&Vstandard_syntax_table);
  1525.   
  1526.   v = XVECTOR (Vstandard_syntax_table);
  1527.   
  1528.   for (i = 'a'; i <= 'z'; i++)
  1529.     v->contents[i] = make_number ((int) Sword);
  1530.   for (i = 'A'; i <= 'Z'; i++)
  1531.     v->contents[i] = make_number ((int) Sword);
  1532.   for (i = '0'; i <= '9'; i++)
  1533.     v->contents[i] = make_number ((int) Sword);
  1534.   v->contents['$'] = make_number ((int) Sword);
  1535.   v->contents['%'] = make_number ((int) Sword);
  1536.   
  1537.   v->contents['('] = make_number ((int) Sopen + (')' << 8));
  1538.   v->contents[')'] = make_number ((int) Sclose + ('(' << 8));
  1539.   v->contents['['] = make_number ((int) Sopen + (']' << 8));
  1540.   v->contents[']'] = make_number ((int) Sclose + ('[' << 8));
  1541.   v->contents['{'] = make_number ((int) Sopen + ('}' << 8));
  1542.   v->contents['}'] = make_number ((int) Sclose + ('{' << 8));
  1543.   v->contents['"'] = make_number ((int) Sstring);
  1544.   v->contents['\\'] = make_number ((int) Sescape);
  1545.   
  1546.   {
  1547.     CONST char *p;
  1548.     for (p = "_-+*/&|<>="; *p; p++)
  1549.       v->contents[(int) *p] = make_number ((int) Ssymbol);
  1550.     
  1551.     for (p = ".,;:?!#@~^'`"; *p; p++)
  1552.       v->contents[(int) *p] = make_number ((int) Spunct);
  1553.   }
  1554. }
  1555.